/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.hq.mes.bussiness.customer.service.impl;

import com.hq.mes.bussiness.customer.domain.Customer;
import me.zhengjie.exception.EntityExistException;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.hq.mes.bussiness.customer.repository.CustomerRepository;
import com.hq.mes.bussiness.customer.service.CustomerService;
import com.hq.mes.bussiness.customer.service.dto.CustomerDto;
import com.hq.mes.bussiness.customer.service.dto.CustomerQueryCriteria;
import com.hq.mes.bussiness.customer.service.mapstruct.CustomerMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author wsq
* @date 2021-12-24
**/
@Service
@RequiredArgsConstructor
public class CustomerServiceImpl implements CustomerService {

    private final CustomerRepository customerRepository;
    private final CustomerMapper customerMapper;

    @Override
    public Map<String,Object> queryAll(CustomerQueryCriteria criteria, Pageable pageable){
        Page<Customer> page = customerRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(customerMapper::toDto));
    }

    @Override
    public List<CustomerDto> queryAll(CustomerQueryCriteria criteria){
        return customerMapper.toDto(customerRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public CustomerDto findById(Long id) {
        Customer customer = customerRepository.findById(id).orElseGet(Customer::new);
        ValidationUtil.isNull(customer.getId(),"Customer","id",id);
        return customerMapper.toDto(customer);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CustomerDto create(CustomerDto resources) {
        if(customerRepository.findByCode(resources.getCode()) != null){
            throw new EntityExistException(Customer.class,"code",resources.getCode());
        }
        return customerMapper.toDto(customerRepository.save( customerMapper.toEntity(resources)));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CustomerDto resources) {
        Customer customer = customerRepository.findById(resources.getId()).orElseGet(Customer::new);
        ValidationUtil.isNull( customer.getId(),"Customer","id",resources.getId());
        Customer customer1 = null;
        customer1 = customerRepository.findByCode(resources.getCode());
        if(customer1 != null && !customer1.getId().equals(customer.getId())){
            throw new EntityExistException(Customer.class,"code",resources.getCode());
        }
        customer.copy( customerMapper.toEntity(resources));
        customerRepository.save(customer);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            customerRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<CustomerDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CustomerDto customer : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("编码", customer.getCode());
            map.put("名称", customer.getName());
            map.put("电话", customer.getTel());
            map.put("联系人", customer.getContact());
            map.put("对公账号", customer.getCompanyAccount());
            map.put("地址", customer.getAddress());
            map.put("备注", customer.getRemark());
            map.put("创建人", customer.getCreateBy());
            map.put("创建时间", customer.getCreateTime());
            map.put("最后修改人", customer.getUpdateBy());
            map.put("最后修改时间", customer.getUpdateTime());
            map.put("是否已删除", customer.getIsDel());
            map.put(" version",  customer.getVersion());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}